package com.platform.hjt.base.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;

import com.platform.hjt.base.costant.SessionConstant;
import com.platform.hjt.sys.model.SysCompanyInfo;
import com.platform.hjt.sys.model.SysDept;
import com.platform.hjt.sys.model.SysUserInfo;

/**
 * 所有Controller的基类
 * </pre>
 * @see
 */
@Controller
public class BaseController {
    protected static final String ERROR_MSG_KEY = "errorMsg";
    
    @Resource
    private HttpServletRequest request;

    /**
     * 获取保存在Session中的用户对象
     *
     * @param request
     * @return
     */
    protected SysUserInfo getSessionUser() {
        return (SysUserInfo) request.getSession().getAttribute(SessionConstant.SESSION_USER);
    }
    
    /**
     * 保存用户对象到Session中
     * @param request
     * @param user
     */
    protected void setSessionUser(SysUserInfo userInfo) {
        request.getSession().setAttribute(SessionConstant.SESSION_USER, userInfo);
    }
    
    protected void removeSessionUser(){
    	 request.getSession().removeAttribute(SessionConstant.SESSION_USER);
    }
    
    protected SysDept getSessionDept() {
       return (SysDept) request.getSession().getAttribute(SessionConstant.SESSION_DEPT);
    }
    
    protected void setSessionDept(SysDept sysDept) {
        request.getSession().setAttribute(SessionConstant.SESSION_DEPT, sysDept);
    }
    
    protected void setSessionCompany(SysCompanyInfo sysCompanyInfo){
		request.getSession().setAttribute(SessionConstant.SESSION_COMPANY, sysCompanyInfo);
	}
    
	public SysCompanyInfo getSessionSysCompanyInfo(){
		return (SysCompanyInfo) request.getSession().getAttribute(SessionConstant.SESSION_COMPANY);
	}
    
    protected void removeSessionDept(){
   	 request.getSession().removeAttribute(SessionConstant.SESSION_DEPT);
   }
    
    protected HttpServletRequest getRequest(){
    	return request;
    }

    /**
     * 获取基于应用程序的url绝对路径
     *
     * @param request
     * @param url
     *            以"/"打头的URL地址
     * @return 基于应用程序的url绝对路径
     */
    public final String getAppbaseUrl(HttpServletRequest request, String url) {
        Assert.hasLength(url, "url不能为空");
        Assert.isTrue(url.startsWith("/"), "必须以/打头");
        return request.getContextPath() + url;
    }
    

}
